package com.ajk.server.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.mime.MultipartEntityBuilder;

public class HttpClientUtil {

	public static String doFormPost(String url, HashMap<String,Object> parameterMap,String ...headers) throws Exception{
		String resData = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String boundary = UUID.randomUUID().toString();
		try {
			HttpPost httppost = new HttpPost(url);

			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){

				for(int i=0; i<headers.length; i=i+2){
					if(headers[i]!=null && headers[i].equalsIgnoreCase("Content-Type")){
						//什么也不做，后面统一添加
					}else{
						httppost.addHeader(headers[i],headers[i+1]);
					}
				}
				httppost.addHeader("Content-Type","multipart/form-data; charset=utf-8; boundary="+boundary);
			}

			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.setBoundary(boundary);
			for(Map.Entry<String, Object> entry:parameterMap.entrySet()){
				String key = entry.getKey();
				Object value = entry.getValue();
				if(value instanceof File){
					builder.addBinaryBody(key,(File)value, ContentType.APPLICATION_OCTET_STREAM, ((File)value).getAbsolutePath());
				}else if(value != null){
					builder.addTextBody(key, value.toString(),ContentType.TEXT_PLAIN);
				}
			}
			HttpEntity reqEntity = builder.build();
			httppost.setEntity(reqEntity);
			CloseableHttpResponse response = httpclient.execute(httppost);

			try {
				HttpEntity resEntity = response.getEntity();
				resData = EntityUtils.toString(resEntity); //会关闭stream。
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
		return resData;
	}

	public static String doGet(String url,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpGet httpGet = null;
		HttpResponse response = null;
		try {
			client = HttpClientBuilder.create().build();
			httpGet = new HttpGet(url);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpGet.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();

			resData = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}


	public static InputStream doGet1(String url,String ...headers){
		CloseableHttpClient client = null;
		HttpGet httpGet = null;
		HttpResponse response = null;
		try {
			client = HttpClientBuilder.create().build();
			httpGet = new HttpGet(url);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpGet.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			return entity.getContent();
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return null;
	}

	public static String doPost(String url,String body,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpPost httpPost = null;
		HttpResponse response = null;
		if(body==null){
			body = "";
		}
		try {
			client = HttpClientBuilder.create().build();
			httpPost = new HttpPost(url);
			StringEntity entity = new StringEntity(body, "UTF-8");//解决中文乱码问题    
			httpPost.setEntity(entity);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpPost.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpPost);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}

	public static String doPut(String url,String body,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpPut httpPut = null;
		HttpResponse response = null;
		if(body==null){
			body = "";
		}
		try {
			client = HttpClientBuilder.create().build();
			httpPut = new HttpPut(url);
			StringEntity entity = new StringEntity(body, "UTF-8");//解决中文乱码问题    
			httpPut.setEntity(entity);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpPut.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpPut);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}

	public static String doDelete(String url,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpDelete httpDelete = null;
		HttpResponse response = null;
		try {
			client = HttpClientBuilder.create().build();
			httpDelete = new HttpDelete(url);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpDelete.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpDelete);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}

}
